package net.lab1024.sa.admin.module.business.goods.task;

import net.lab1024.sa.admin.module.business.category.service.CategoryService;
import net.lab1024.sa.admin.module.business.goods.constant.GoodsStatusEnum;
import net.lab1024.sa.admin.module.business.goods.domain.form.GoodsAddForm;
import net.lab1024.sa.admin.module.business.goods.domain.form.OtherGroupQueryForm;
import net.lab1024.sa.admin.module.business.goods.domain.vo.OtherGroupVO;
import net.lab1024.sa.admin.module.business.goods.service.GoodsService;
import net.lab1024.sa.admin.module.business.goods.service.OtherGroupService;
import net.lab1024.sa.common.common.domain.PageResult;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.module.support.jdunion.JdUnionService;
import net.lab1024.sa.common.module.support.jdunion.domain.vo.GoodsInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import net.lab1024.sa.common.module.support.jdunion.domain.form.*;

import java.math.BigDecimal;
import java.util.Arrays;


@Component
public class SyncGroupGoodsTask {


    @Autowired
    private OtherGroupService otherGroupService;

    @Autowired
    private JdUnionService jdUnionService;


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsService goodsService;

    @Scheduled(fixedRate = 3600000)
    public void run() {

        OtherGroupQueryForm otherGroupQueryForm=new OtherGroupQueryForm();
        otherGroupQueryForm.setPageNum(1);
        otherGroupQueryForm.setPageSize(100);
        final PageResult<OtherGroupVO> otherGroupVOPageResult = otherGroupService.queryPage(otherGroupQueryForm);

        for(OtherGroupVO otherGroupVO: otherGroupVOPageResult.getList()) {

            Integer page = 1;

            do {

                GoodsInfoQueryForm goodsInfoQueryForm = new GoodsInfoQueryForm();

                goodsInfoQueryForm.setGroupNumber(otherGroupVO.getGroupNumber());

                goodsInfoQueryForm.setChannelNumber(otherGroupVO.getChannelNumber());

                goodsInfoQueryForm.setPageNum(page);

                page++;

                goodsInfoQueryForm.setPageSize(100);

                ResponseDTO<PageResult<GoodsInfoVO>> responseDTO = jdUnionService.getGoodsByGroup(goodsInfoQueryForm);

                if(!responseDTO.getOk() || responseDTO.getData().getEmptyFlag()){
                    break;
                }


                for (GoodsInfoVO goodsInfoVO : responseDTO.getData().getList()) {
                    //查询分类ID
                    ResponseDTO<String> categoryRsp = categoryService.queryCategoryId(goodsInfoVO.getCid3());
                    if (categoryRsp.getCode().equals(ResponseDTO.OK_CODE)) {
                        Long categoryId = Long.parseLong(categoryRsp.getData());

                        //添加商品
                        GoodsAddForm goodsAddForm = new GoodsAddForm();
                        goodsAddForm.setCategoryId(categoryId);
                        goodsAddForm.setGoodsName(goodsInfoVO.getSkuName());
                        goodsAddForm.setGoodsStatus(GoodsStatusEnum.SELL.getValue());
                        goodsAddForm.setPrice(new BigDecimal(goodsInfoVO.getPrice().toString()));
                        goodsAddForm.setShelvesFlag(false);
                        goodsAddForm.setOwner(goodsInfoVO.getOwner());
                        goodsAddForm.setComments(goodsInfoVO.getComments());
                        goodsAddForm.setGoodCommentsShare(goodsInfoVO.getGoodCommentsShare());
                        goodsAddForm.setImgUrl(Arrays.stream(goodsInfoVO.getImgUrls()).findFirst().orElse(""));
                        goodsAddForm.setCommissionShare(goodsInfoVO.getCommissionShare());
                        goodsAddForm.setOtherId(goodsInfoVO.getSkuId());
                        ResponseDTO<String> promoteUrlRsp = jdUnionService.getPromoteUrl(goodsInfoVO.getMaterialUrl());
                        if (promoteUrlRsp.getOk()) {
                            goodsAddForm.setPurchaseLink(promoteUrlRsp.getData());

                        }

                        goodsService.add(goodsAddForm);

                    }

                }


            }while(true);
        }

        System.out.println("同步选品库商品定时任务执行...");


    }
}
